package com.swingson.properties.impl;

import com.swingson.SwingsonGui;
import com.swingson.exceptions.SwingsonException;
import com.swingson.jsonparser.JsonArray;
import com.swingson.jsonparser.JsonElement;
import com.swingson.properties.BasePropertyProcessor;
import java.awt.Dimension;
import javax.swing.JComponent;

/**
 * @author sad
 */
public class DimensionPropertyProcessor extends BasePropertyProcessor {

    @Override
    public Object processProperty(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
        if (!jsonElement.isArray()) {
            throw new SwingsonException(String.format("Property '%s' should be jsonarray value with 2 elements.", propertyName));
        }

        JsonArray array = jsonElement.getAsArray();
        if (array.size() != 2) {
            throw new SwingsonException(String.format("Property '%s' should be jsonarray value with 2 elements.", propertyName));
        }
        int width=getIntValue(array.getElements().get(0), String.format("Width in dimension property '%s' should be integer value", propertyName));
        int height=getIntValue(array.getElements().get(1),String.format("Width in dimension property '%s' should be integer value", propertyName));
        return new Dimension(width, height);
    }
}
